package com.util;

public class LoggerHelper {
	
	private static final Integer LOG_STRACKTRACE_LENGTH = 100;
	
	/**
	 * 找到调用类的相关信息，
	 * level 最小为 2， 即为直接调用本类的类信息
	 * @param level
	 * @return
	 */
	public static String whoInvokedMe(Integer level){
		String ret = "UNKNOWN";
		StackTraceElement[] element = Thread.currentThread().getStackTrace();
		if(level != null && element.length > level){
			if(level < 2){
				level = 2;
			}
			ret = "["+element[level].getClassName()+"."+element[level].getMethodName()+"()#"+element[level].getLineNumber()+"]";
		}else{
			//获得全部调用信息
			ret = "";
			for (int i = 1; i < element.length; i++) {
				ret += "["+element[i].getClassName()+"."+element[i].getMethodName()+"()#"+element[i].getLineNumber()+"]";
				if(ret.length() > LOG_STRACKTRACE_LENGTH){
					ret += "...";
					break;
				}
				if(i+1 < element.length){
					ret += "<==";
				}
			}
			ret += "\n";
		}
		return ret;
	}

}
